home *** CD-ROM | disk | FTP | other *** search
/ TPUG - Toronto PET Users Group / TPUG Users Group CD / TPUG Users Group CD.iso / AMIGA / AMICUS / AMICUS08.ADF / C / PDScreenDump.c < prev    next >
C/C++ Source or Header  |  1986-04-02  |  14KB  |  545 lines

  1. /**************************************************************************
  2. *  ScreenDump.c --  Dump RastPort of highest screen/window to printer
  3. *                    by Carolyn Scheppner  CBM  02/86
  4. *
  5. * Linkage information:
  6. * FROM     LStartup.obj, ScreenDump.o
  7. * TO       ScreenDump
  8. * LIBRARY  LC.lib, Amiga.lib
  9. ***************************************************************************/
  10.  
  11. #include <exec/types.h>
  12. #include <exec/memory.h>
  13. #include <libraries/dos.h>
  14. #include <graphics/gfxbase.h>
  15. #include <graphics/rastport.h>
  16. #include <graphics/gfx.h>
  17. #include <graphics/view.h>
  18.  
  19. #include <intuition/intuition.h>
  20. #include <intuition/intuitionbase.h>
  21.  
  22. #include <devices/printer.h>
  23. #include <lattice/stdio.h>
  24.  
  25. struct IntuitionBase *IntuitionBase;
  26. struct GfxBase       *GfxBase;
  27.  
  28. struct Window   *firstWindow, *nextWindow, *highWindow;
  29. struct Screen   *firstScreen, *nextScreen, *highScreen;
  30.  
  31. struct ViewPort *viewPort;
  32. struct RastPort *rastPort;
  33. struct ColorMap *colorMap;
  34.  
  35. SHORT  topEdge;
  36.  
  37. BOOL   Borderless, WindowPrint, Relative, pOpened;
  38. int    dLeft, dRight, dTop, dBottom;   /* default border adjustments */
  39. int    bLeft, bRight, bTop, bBottom;   /* current border adjustments */
  40. int    srcX, srcY, srcWidth, srcHeight;
  41. int    destCols, destRows, special;
  42. int    openError, dumpError, multiplier, i;
  43. char   sbuf[80], ch;
  44.  
  45. struct MsgPort   *printerPort;
  46.  
  47. union  printerIO {
  48.           struct IOStdReq    ios;
  49.           struct IODRPReq    iodrp;
  50.           struct IOPrtCmdReq iopc;
  51.           };
  52.  
  53. union printerIO  *request;
  54.  
  55.  
  56. /* main */
  57.  
  58. main()
  59.    {
  60.    if ((IntuitionBase =
  61.       (struct IntuitionBase *)OpenLibrary("intuition.library",0))==NULL)
  62.    cleanexit("Can't open Intuition.\n");
  63.  
  64.    if ((GfxBase =
  65.       (struct GfxBase *)OpenLibrary("graphics.library",0))==NULL)
  66.    cleanexit("Can't open Graphics.\n");
  67.  
  68.  
  69.    /* Printer stuff */
  70.    pOpened = FALSE;
  71.    printerPort = (struct MsgPort *)CreatePort("myPrtPort",0);
  72.    if(printerPort == NULL)  cleanexit("Can't open printer port.\n");
  73.  
  74.    request = (union printerIO *)CreateExtIO(printerPort,
  75.                                              sizeof(union printerIO));
  76.    if(request == NULL)  cleanexit("Can't allocate ExtIO.\n");
  77.  
  78.    openError = OpenPrinter(request);
  79.    if(openError)  cleanexit("Can't open printer.\n");
  80.    pOpened = TRUE;
  81.    /* */
  82.  
  83.    printf("\n>> SCREENDUMP <<       C. Scheppner     CBM  02/86\n");
  84.    printf("\nDefault settings: Full Image, Relative Size Output\n");
  85.  
  86.    WindowPrint = FALSE;
  87.    Borderless  = FALSE;
  88.    Relative   = TRUE;
  89.    dLeft   = 2;                  /* defaults for Borderless option */
  90.    dRight  = 2;
  91.    dTop    = 10;
  92.    dBottom = 10;
  93.    multiplier = 125;             /* default for Relative calculation */
  94.  
  95.    newImage();        /* get pointers to rastport, etc */
  96.  
  97.    do {
  98.       printf("\nSELECT: <P>rint, <I>mageOpts, <O>utputOpts,");
  99.       printf(" <N>ewImage or <Q>uit ? ");
  100.       getLch();
  101.       if (ch =='p')       doPrint();
  102.       else if (ch =='i')  doImOpts();
  103.       else if (ch =='o')  doOutOpts();
  104.       else if (ch =='n')  newImage();
  105.       }
  106.    while (ch != 'q');
  107.  
  108.    cleanup();
  109.    }
  110.  
  111. /*--------------------------------------------------------------------*/
  112.  
  113. newImage()
  114.    {
  115.    printf("\nNEW IMAGE: <W>indow or <S>creen ? ");
  116.    getLch();
  117.    if (ch =='w')
  118.       {
  119.       WindowPrint = TRUE;
  120.       printf("\nMove Screens so Windows's Screen is highest.\n");
  121.       printf("Move Windows so Window to print is highest.\n");
  122.       }
  123.    else if (ch =='s')
  124.       {
  125.       WindowPrint = FALSE;
  126.       printf("\nMove Screens so Screen to print is highest.\n");
  127.       }
  128.    ch = NULL;
  129.  
  130.    printf("\nClick here and press <RET> when ready: ");
  131.    getLch();
  132.  
  133.    getPointers();
  134.    }
  135.  
  136.  
  137. getPointers()
  138.    {
  139.    Forbid();
  140.    firstScreen  = IntuitionBase->FirstScreen;
  141.    topEdge = 400;
  142.    nextScreen = highScreen = firstScreen;
  143.    while( nextScreen != NULL )
  144.       {
  145.       if(nextScreen->TopEdge < topEdge)
  146.          {
  147.          topEdge = nextScreen->TopEdge;
  148.          highScreen = nextScreen;
  149.          }
  150.       nextScreen = nextScreen->NextScreen;
  151.       }
  152.  
  153.    if(WindowPrint)
  154.       {
  155.       firstWindow = highScreen->FirstWindow;
  156.       topEdge = 400;
  157.       nextWindow = highWindow = firstWindow;
  158.       while( nextWindow != NULL )
  159.          {
  160.          if(nextWindow->TopEdge < topEdge)
  161.             {
  162.             topEdge = nextWindow->TopEdge;
  163.             highWindow = nextWindow;
  164.             }
  165.          nextWindow = nextWindow->NextWindow;
  166.          }
  167.       }
  168.    Permit();
  169.  
  170.    viewPort = &(highScreen->ViewPort);
  171.    colorMap = viewPort->ColorMap;
  172.    rastPort = &(highScreen->RastPort);
  173.  
  174.    getCoords();
  175.    }
  176.  
  177.  
  178. getCoords()
  179.    {
  180.    if(WindowPrint)   /* if Borderless == FALSE, b variables = 0 */
  181.       {
  182.       srcX = highWindow->LeftEdge + bLeft;
  183.       srcY = highWindow->TopEdge + bTop;
  184.       srcWidth  = highWindow->Width -bLeft - bRight;
  185.       srcHeight = highWindow->Height -bTop - bBottom;
  186.       }
  187.    else      /* screen print */
  188.       {
  189.       srcX = bLeft;
  190.       srcY = bTop;
  191.       srcWidth  = viewPort->DWidth -bLeft - bRight;
  192.       srcHeight = viewPort->DHeight - bTop - bBottom;
  193.       }
  194.    if (Relative == TRUE)
  195.       {
  196.       doRelativeSize();
  197.       }
  198.    }
  199.  
  200.  
  201. doImOpts()
  202.    {
  203.    printf("\nIMAGE OPTS: <F>ull, <B>orderless, or <O>ther ? ");
  204.    getLch();
  205.    if (ch == 'f')
  206.       {
  207.       Borderless = FALSE;
  208.       bLeft = bRight = bTop = bBottom = 0;
  209.       getCoords();
  210.       }
  211.    else if (ch == 'b')
  212.       {
  213.       Borderless = TRUE;
  214.  
  215.       printf("\nBORDERS: Change default borders <Y> or <N> ? ");
  216.       getLch();
  217.       if (ch == 'y')
  218.          {
  219.          printf("\nBORDER ADJUSTMENTS: Enter new value or press <RET>\n");
  220.          dLeft   = promptInt("Left",dLeft);
  221.          dRight  = promptInt("Right",dRight);
  222.          dTop    = promptInt("Top",dTop);
  223.          dBottom = promptInt("Bottom",dBottom);
  224.          }
  225.       bLeft   = dLeft;
  226.       bRight  = dRight;
  227.       bTop    = dTop;
  228.       bBottom = dBottom;
  229.       getCoords();
  230.       ch = NULL;
  231.       }
  232.    else if (ch == 'o')
  233.       {
  234.       otherImOpts();
  235.       }
  236.    ch = NULL;
  237.    }   
  238.  
  239.  
  240. otherImOpts()
  241.    {
  242.    printf("\nOTHER IMAGE OPTIONS:\n");
  243.    printf(" CURRENT OPTIONS: ");
  244.    if (!Borderless) printf("Full ");
  245.     else printf("Borderless ");
  246.    if (!WindowPrint) printf("Screen\n");
  247.     else printf("Window\n");
  248.  
  249.    printf("\nCURRENT VALUES: Enter new value or press <RET>\n");
  250.    srcX = promptInt("SrcX",srcX);
  251.    srcY = promptInt("SrcY",srcY);
  252.    srcWidth  = promptInt("SrcWidth",srcWidth);
  253.    srcHeight = promptInt("SrcHeight",srcHeight);
  254.    }
  255.  
  256.  
  257. doOutOpts()
  258.    {
  259.    printf("\nOUTPUT OPTS: <S>mall, <M>edium, <L>arge,");
  260.    printf(" <R>elative or <O>ther ? ");
  261.    getLch();
  262.  
  263.    if (ch == 's')
  264.       {
  265.       Relative = FALSE;
  266.       special  = SPECIAL_FRACCOLS|SPECIAL_ASPECT;
  267.       destCols = 0x55555555;    /* 1/3 size */
  268.       destRows = 0;
  269.       }
  270.    else if (ch == 'm')
  271.       {
  272.       Relative = FALSE;
  273.       special  = SPECIAL_FRACCOLS|SPECIAL_ASPECT;
  274.       destCols = 0xaaaaaaaa;    /* 2/3 size */
  275.       destRows = 0;
  276.       }
  277.    else if (ch == 'l')
  278.       {
  279.       Relative = FALSE;
  280.       special  = SPECIAL_FULLCOLS|SPECIAL_ASPECT;
  281.       destCols = 0;
  282.       destRows = 0;
  283.       }
  284.    else if (ch == 'r')
  285.       {
  286.       askMultiplier();
  287.       doRelativeSize();
  288.       }
  289.    else if (ch == 'o')
  290.       {
  291.       otherOutOpts();
  292.       }
  293.    ch = NULL;
  294.    }   
  295.  
  296.  
  297. askMultiplier()
  298.    {
  299.    printf("\nRELATIVE: Change default multiplier <Y> or <N> ? ");
  300.    getLch();
  301.    if (ch == 'y')
  302.       {
  303.       printf("\nMULTIPLIER: Enter new value or press <RET>\n");
  304.       multiplier = promptInt("Multiplier",multiplier);
  305.       }
  306.    ch = NULL;
  307.    }
  308.  
  309.  
  310. doRelativeSize()
  311.    {
  312.    Relative = TRUE;
  313.    special  = SPECIAL_ASPECT|SPECIAL_MILCOLS;
  314.    destCols = (int) srcWidth * multiplier / 10;
  315.    if ((viewPort->Modes & HIRES) == NULL) destCols = destCols * 2;
  316.    destRows = 0;
  317.    }
  318.  
  319.  
  320. otherOutOpts()
  321.    {
  322.    printf("\nSpecial Flags Available:\n");
  323.    printf(" MILCOLS   0x001   DestCols specified in 1/1000 inch\n");
  324.    printf(" MILROWS   0x002   DestRows specified in 1/1000 inch\n");
  325.    printf(" FULLCOLS  0x004   Make DestCols maximum possible\n");
  326.    printf(" FULLROWS  0x008   Make DestRows maximum possible\n");
  327.    printf(" FRACCOLS  0x010   DestCols is fraction of FULLCOLS\n");
  328.    printf(" FRACROWS  0x020   DestRows is fraction of FULLROWS\n");
  329.    printf(" ASPECT    0x080   Ensure correct aspect ratio\n");
  330.    printf(" DENSITY1  0x100   Lowest Res\n");
  331.    printf(" DENSITY2  0x200   Next Res\n");
  332.    printf(" DENSITY3  0x400   Next Res\n");
  333.    printf(" DENSITY4  0x800   Highest Res\n");
  334.    printf(" MASK      0xF00   Mask out density bits\n");
  335.  
  336.    printf("\nCURRENT VALUES: Enter new value or press <RET>\n");
  337.    destCols = promptInt("DestCols",destCols);
  338.    destRows = promptInt("DestRows",destRows);
  339.    special  = promptInt("Special",special);
  340.    }
  341.  
  342.  
  343. promptInt(stp,value)   /* Prompt for and input new value */
  344. char   *stp;
  345. int    value;
  346.    {
  347.    printf(" %s = %ld  [ Hex 0x%lx ]   New value ? ",stp,value,value);
  348.    value = getInt(value);
  349.    return(value);
  350.    }
  351.  
  352. getLch()     /* ch = input char converted to lower case or NULL */
  353.    {
  354.    gets(&sbuf[0]);
  355.    if ((ch=(sbuf[0])) != NULL)  ch = ch|0x20;
  356.    }
  357.  
  358. getInt(value)         /* Get decimal or hex (0xnnnn) value */
  359. int value;            /* If none entered, return previous  */
  360.    {
  361.    int count;
  362.  
  363.    gets(&sbuf[0]);
  364.    if (sbuf[0] != NULL)
  365.       {
  366.       if ((sbuf[1]|0x20) =='x') count = stch_i(&sbuf[2],&value);
  367.       else count = stcd_i(&sbuf[0],&value);
  368.       }
  369.    return(value);
  370.    }
  371.  
  372.  
  373.  
  374. doPrint()
  375.    {
  376.    printf("\nPrinting will start in 10 seconds.\n");
  377.    printf(" ( Time to hide this window, etc. )\n\n");
  378.    for(i=0;i<10*60;i++) WaitTOF();  /* 10 second delay */
  379.  
  380.    /* Printer stuff */
  381.    dumpError = DumpRPort( request,
  382.                           rastPort,
  383.                           colorMap,
  384.                           viewPort->Modes,
  385.                           srcX,                       /* RastPort offsets */
  386.                           srcY,
  387.                           srcWidth,                   /* origin sizes  */
  388.                           srcHeight,
  389.                           destCols,                   /* printed sizes */
  390.                           destRows,
  391.                           special );                  /* io_Special */
  392.  
  393.    if (dumpError)  printf("\n Can't Dump. PDERR = %ld\n",dumpError);
  394.    }
  395.  
  396.  
  397. cleanexit(errMsg)
  398. char *errMsg;
  399.    {
  400.    printf(errMsg);
  401.    cleanup();
  402.    exit(-1);
  403.    }
  404.  
  405. cleanup()
  406.    {
  407.    pcleanup();
  408.    if (IntuitionBase)     CloseLibrary(IntuitionBase);
  409.    if (GfxBase)           CloseLibrary(GfxBase);
  410.    }
  411.  
  412. pcleanup()
  413.    {
  414.    if (pOpened)           ClosePrinter(request);
  415.    if (printerPort)       DeletePort(printerPort);
  416.    if (request)           DeleteExtIO(request);
  417.    }
  418.  
  419.  
  420. /************************************************************************/
  421. /* printersupport.c rtns  for example page 3-202  1.1 Rom Kernel manual */
  422. /************************************************************************/
  423.  
  424. #include        "exec/types.h"
  425. #include        "exec/nodes.h"
  426. #include        "exec/lists.h"
  427. #include        "exec/ports.h"
  428. #include        "exec/tasks.h"
  429. #include        "exec/io.h"
  430. #include        "devices/printer.h"
  431.  
  432.  
  433. /*  This union is defined above in my main
  434.     If you are compiling these rtns separately,
  435.      uncomment this union definition
  436.  
  437. union printerIO {
  438.        struct IOStdReq ios;
  439.        struct IODRPReq iodrp;
  440.        struct IOPrtCmdReq iopc;
  441.        };
  442. */
  443.  
  444.  
  445. /* OPEN THE PRINTER */
  446. int
  447. OpenPrinter(request)
  448.    union printerIO *request;
  449.       {
  450.       return(OpenDevice("printer.device",0,request,0));
  451.       }
  452.  
  453.  
  454. /* CLOSE THE PRINTER */
  455. int
  456. ClosePrinter(request)
  457.    union printerIO *request;
  458.       {
  459.       CloseDevice(request);
  460.       return(0);
  461.       }
  462.  
  463.  
  464. /* Send a NULL terminated string to the printer
  465.  * Assumes printer device is open and printerMsg
  466.  * is correctly initialized.  Watches for embedded
  467.  * "escape-sequences" and handles them as defined.
  468.  */
  469.  
  470. int
  471. PrintString(request,string)
  472.    union printerIO *request;
  473.    char *string;
  474.    {
  475.    request->ios.io_Command = CMD_WRITE;
  476.    request->ios.io_Data = (APTR)string;
  477.    request->ios.io_Length = -1;
  478.    /* if -1, the printer assumes it has been given
  479.     * a null terminated string.
  480.     */
  481.    return(DoIO(request));
  482.    }
  483.  
  484.  
  485. /* Send RAW character stream to the printer directly,
  486.  * avoid "escape-sequence" parsing by the device.
  487.  */
  488. int
  489. PrintRaw(request,buffer,count)
  490.    union printerIO *request;  /* a properly initialized request block */
  491.    char *buffer;              /* where is the output stream of characters */
  492.    int count;                 /* how many characters to output */
  493.       {
  494.       /* queue a printer raw write */
  495.       request->ios.io_Command = PRD_RAWWRITE;
  496.       request->ios.io_Data = (APTR)buffer;
  497.       request->ios.io_Length = count;
  498.  
  499.       return(DoIO(request));
  500.       }
  501.  
  502.  
  503. /* Send Printer Command */
  504. int
  505. PrintCommand(request,command, p0, p1, p2, p3)
  506.    union printerIO *request;
  507.    int command, p0, p1, p2, p3;    /* command and its parameters */
  508.       {
  509.       /* queue a printer command */
  510.       request->iopc.io_Command = PRD_PRTCOMMAND;
  511.       request->iopc.io_PrtCommand = command;
  512.       request->iopc.io_Parm0 = p0;
  513.       request->iopc.io_Parm1 = p1;
  514.       request->iopc.io_Parm2 = p2;
  515.       request->iopc.io_Parm3 = p3;
  516.       return(DoIO(request));
  517.       }
  518.  
  519.  
  520. /* Dump RastPort */
  521. int
  522. DumpRPort(request,rastPort, colorMap, modes, sx,sy, sw,sh, dc,dr, s)
  523.    union printerIO *request;
  524.    struct RastPort *rastPort;
  525.    struct ColorMap *colorMap;
  526.    ULONG modes;
  527.    UWORD sx, sy, sw, sh;
  528.    LONG dc, dr;
  529.    UWORD s;
  530.       {
  531.       request->iodrp.io_Command = PRD_DUMPRPORT;
  532.       request->iodrp.io_RastPort = rastPort;
  533.       request->iodrp.io_ColorMap = colorMap;
  534.       request->iodrp.io_Modes = modes;
  535.       request->iodrp.io_SrcX = sx;
  536.       request->iodrp.io_SrcY = sy;
  537.       request->iodrp.io_SrcWidth = sw;
  538.       request->iodrp.io_SrcHeight = sh;
  539.       request->iodrp.io_DestCols = dc;
  540.       request->iodrp.io_DestRows = dr;
  541.       request->iodrp.io_Special = s;
  542.       return(DoIO(request));
  543.       }
  544.  
  545.